function [ EC ] = APEgenerateoverview( datasetname )
%[ EC ] = APEgenerateoverview( datasetname )
%
% Generate overview and savees as PDF
% Contain following plots and informations:
%       1) Filename
%       2) RAW data
%       3) Ramanshifted Data
%       4) ALS corrected Data
%       5) Footer conversion parameters
%
% INPUT     datasetname    : filename of APE-MAT file to process
%
% OUTPUT    EC          :   error code (0 = success, 1 = error)

%  (c) F. Dworkowski, 2011
%      Version 1.0, 2011-09-26

if nargin<1
    help APEgenerateoverview
    EC = 1;
    return
end

load(datasetname)

%% ===== Draw Figure =====
set(0,'units', 'pixels');
% scrsz = get(0,'ScreenSize');
%     fhight = (scrsz(4)-150);
%     fwidth = (fhight*0.707);
%     fleft  = (scrsz(3)-fwidth)/2;

A4=figure(453);
%    set(A4, 'Position',[fleft 50 fwidth fhight], ...
    set(A4, 'Units','centimeters',...
        'Position',[12 1 20 29],...
        'Name', 'APE Data Overview', ...
        'PaperType', 'A4',...
        'Color',[1 1 1], ...
        'PaperUnits', 'inches', ...
        'PaperPosition', [0 0 1 1], ...
        'PaperOrientation','portrait')

% ----- print header -----
    [pathstr, name, ext] = fileparts(footer.FileName);
    annotation('textbox',[0.1 0.94 0.8 0.05],...
        'FontSize', 10, 'FontWeight', 'demi', ...
        'Interpreter','none', 'LineStyle', 'none', ...
        'string',{['Generated on ',date];['FILE: ',name,ext]})
    logobox = axes('Position',[0.6 0.95 0.33 0.05]);
        imshow('slspectrolab.jpg', 'Parent', logobox)
        
% ----- print footer -----
    annotation('textbox',[0.09 0.01 0.8 0.05],...
        'FontSize', 8, ...
        'Interpreter','none', 'LineStyle', 'none', ...
        'string',{['Original folder: ',pathstr]; ...
                ['Output folder : ',cd]})

%% ===== PLOT DATA =====  
if exist('ramandatax','var')==1     % plot Raman-type Spectra
    subplot(4,1,1)
        plot(datax,datay)
            title('Raw data','FontWeight', 'demi')
            xlabel('Wavelength (nm)')
            ylabel('counts (AU)')

    subplot(4,1,2)
        if exist('ramandatax','var')==1        
            plot(ramandatax,datay)
                title('Raman shifted','FontWeight', 'demi')
                xlabel('Raman shift (cm^-^1)')
                ylabel('counts (AU)')
        end
else                                % plot non-Raman Spectra
    subplot(4,1,[1:2])
        plot(datax,datay)
            title('Raw data','FontWeight', 'demi')
            xlabel('Wavelength (nm)')
            ylabel('counts (AU)')
end

% Only plot 3rd graph if ALS corrected data is present
if exist('ALSdatay','var')==1
    hold on
        subplot(4,1,2)
        plot(ramandatax, ALSbasey,'r--')
    hold off
    subplot(4,1,3)
        if exist('ALSdatay','var')==1        
            plot(ramandatax,ALSdatay,'r')
                title('ALS baseline corrected','FontWeight', 'demi')
                xlabel('Raman shift (cm^-^1)')
                ylabel('Intensity (AU)')
        end    
end

%% ===== Generate Parameter Table(s) =====
footertemp(1,:) = fieldnames(footer);
footertemp(2,:) = struct2cell(footer);    

footerout1 = footertemp(:,1:12)';
footerout2 = footertemp(:,13:end)';

if exist('ALSdatay','var')==1
    temp = [ALSpara{1,2} ALSpara{2,2} ALSpara{3,2}];
    addtotable = {'ALS parameter', num2str(temp)};
    footerout2 = [footerout2; addtotable];
end

if exist('Laser_exwavelength','var')==1
    addtotable = {'Excitation Laser', num2str(Laser_exwavelength)};
    footerout2 = [footerout2; addtotable];
end
    

bottomtable1 = uitable('Parent', A4,...
    'Units','centimeters','Position',[2 2 8 6], ...
    'FontSize',7,...
    'ColumnWidth',{140}, ...
    'ColumnName',[{'Type' 'Value'}], ...
    'RowName',[{}], ...
    'Data', footerout1);

bottomtable2 = uitable('Parent', A4,...
    'Units','centimeters','Position',[10 2 8 6], ...
    'FontSize',7,...
    'ColumnWidth',{140}, ...
    'ColumnName',[{'Type' 'Value'}], ...
    'RowName',[{}], ...
    'Data', footerout2);

backgroundColor = [0.95 0.95 0.95; .9 .9 .9];
set(bottomtable1, 'BackgroundColor', backgroundColor);
set(bottomtable2, 'BackgroundColor', backgroundColor);


%% ===== SAVE PDF ===== 
% make sure to save to the atarting path, not nececarrily th data path
[~, puredatasetname, ext] = fileparts(datasetname); 
    % save .fig
    saveas(gcf,[puredatasetname,'_overview.fig'])
    % save PDF
    orient tall
    print('-dpdf','-painters',[puredatasetname,'_overview'])
    set(0,'units', 'pixels');

